package com.siwei.mes.controller.common;

import com.siwei.mes.common.annotation.AnonymousAccess;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.AddGroup;
import com.siwei.mes.common.validator.PageGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.entity.DictValue;
import com.siwei.mes.service.DictValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 字典值表相关
 *
 * @author linzi
 * @since 2023-11-25 22:22:21
 */
@Api(value = "字典值表")
@RestController
@RequestMapping("dictValue")
public class DictValueController {
    @Resource
    private DictValueService dictValueService;

    @ApiOperation(value = "新增字典值表")
    @PostMapping("/create")
    public Json create(@RequestBody @Validated(AddGroup.class) DictValue entity) {
        return Json.succ().data(ApiResult.addColumn("id", dictValueService.create(entity)));
    }

    @ApiOperation(value = "修改字典值表")
    @PostMapping("/update")
    public Json update(@RequestBody @Validated(UpdateGroup.class) DictValue entity) {
        dictValueService.update(entity);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "删除字典值表")
    @PostMapping("/delete")
    public Json delete(@RequestBody DictValue entity) {
        dictValueService.delete(entity.getId());
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "分页获取字典值表")
    @PostMapping("/page")
    public Json appPage(@RequestBody @Validated(PageGroup.class) PageQuery<DictValue, DictValue> query) {
        return Json.succ().data(dictValueService.getPage(query));
    }

    @ApiOperation(value = "根据主键id获取字典值表")
    @GetMapping("loadById")
    public Json loadById(@RequestParam("id") Long id) {
        return Json.succ().data(dictValueService.loadById(id));
    }

    @ApiOperation(value = "获取所有字典值表")
    @PostMapping("getAll")
    public Json getAll(@RequestBody DictValue entity) {
        return Json.succ().data(ApiResult.addList(dictValueService.getAll(entity)));
    }

    @AnonymousAccess
    @PostMapping("/getDictValue")
    public Json getDictValue(@RequestBody DictValue entity) {
        assertTrue(StringUtils.isBlank(entity.getDictCode()), "字典code为空");
        return Json.succ().data(ApiResult.addList(dictValueService.getAll(entity)));
    }

    @AnonymousAccess
    @PostMapping("/batchDictValue")
    public Json batchDictValue(@RequestBody DictValue entity) {
        if (entity.getDictCodeList() == null || entity.getDictCodeList().isEmpty()) {
            return Json.fail().msg("查询字典code列表为空");
        }
        return Json.succ().data(dictValueService.batchDictValue(entity));
    }

}
